home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / util / misc / sortham.lzh / sortham.c next >
Encoding:
C/C++ Source or Header  |  1992-06-15  |  4.7 KB  |  251 lines

  1.  
  2. /*    sortham
  3.       o  determine size of input file.
  4.       o  assume a worst case with lines containing only 1 x 2 call signs.
  5.       o  alloc a memory buffer big enough to hold the input file.
  6.       o  alloc a memory buffer big enough to hold a pointer to each line.
  7.       o  call manx's quicksort to do the work.
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <fcntl.h>
  13. #include <ctype.h>
  14. #include <stdlib.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17.  
  18. static     char  *VERSION="$VER: sortham 1.0 Vince Herried KA8CTE (30.05.92)";
  19.  
  20. static     char *buf=NULL,*pbuf=NULL,**p,line[256];
  21. static     FILE  *input=NULL,*output=NULL;
  22.  
  23. void cleanup(void);
  24.  
  25. int fsize (char *name)
  26.  
  27. {
  28.    struct stat stbuf;
  29.  
  30.    if ( stat(name,&stbuf) == -1 )
  31.    {
  32.       fprintf(stderr,"SRTH001E: can't access %s\n",name);
  33.       return(-1);
  34.    } /* if stat(name,&stbuf) */
  35.  
  36. /*   if ( (stbuf.st_mode & S_IFMT) == S_IFDIR )
  37.    {
  38.  
  39.    } /* if (stbuf.st_mode */
  40.    return(stbuf.st_size);
  41. } /* fsize () */
  42.  
  43. cmp (char **a,char **b)
  44. {
  45.    char  ta[7],tb[7];
  46.    char  *x;
  47.    int     i,j;
  48.  
  49.    strcpy(ta,"      "); /* initialize work strings */
  50.    strcpy(tb,"      "); /* ditto */
  51.  
  52.    /* look for the digit (assumes U. S. format call sign) */
  53.    /* build sort key with prefix stuck on the end     */
  54.  
  55.    x = *a;
  56.    for (i = 0; i<2;i++ )
  57.    {
  58.       if ( isdigit(*x) ) break;
  59.       ta[i+4] = toupper(*x);
  60.       x++;
  61.    } /* for  */
  62.  
  63.    ta[0]=toupper(*x);
  64.    i++;
  65.    x++;
  66.    j = 1;
  67.    for (;i < 6;i++ )
  68.    {
  69.       if ( ! isalpha(*x) ) break;
  70.       ta[j] = toupper(*x);
  71.       j++;
  72.       x++;
  73.    } /* for  */
  74.  
  75.    x = *b;
  76.    for (i = 0; i<2;i++ )
  77.    {
  78.       if ( isdigit(*x) ) break;
  79.       tb[i+4] = toupper(*x);
  80.       x++;
  81.    } /* for  */
  82.  
  83.    tb[0]=toupper(*x);
  84.    i++;
  85.    x++;
  86.    j = 1;
  87.    for (;i < 6;i++ )
  88.    {
  89.       if ( ! isalpha(*x) ) break;
  90.       tb[j] = toupper(*x);
  91.       j++;
  92.       x++;
  93.    } /* for  */
  94.  
  95. #if DEBUG
  96.    pprintf(stderr,"SRTH002I - ta =%s, tb =%s\n",ta,tb);
  97. #endif
  98.  
  99.    return strcmp(ta,tb);
  100.  
  101. } /* cmp () */
  102.  
  103.  
  104. void cleanup ()
  105.  
  106. {
  107.    if ( !buf )
  108.    {
  109.       free(buf);
  110.    } /* if !buf */
  111.  
  112.    if ( !pbuf )
  113.    {
  114.       free(pbuf);
  115.    } /* if !pbuf */
  116.  
  117.    if ( !input )
  118.    {
  119.       fclose(input);
  120.    } /* if !input */
  121.  
  122.    if ( !output )
  123.    {
  124.       fclose(output);
  125.    } /* if !output */
  126.  
  127.    exit(0);
  128.  
  129. } /* cleanup () */
  130.  
  131.  
  132.  
  133. int main (int argc,char **argv)
  134. {
  135.    int     size,i,numlines,numptrs,cmp();
  136.    int     buf_used,ptrs_used;
  137.  
  138.    if ( argc != 3 )     /* default: current directory */
  139.    {
  140.       fprintf(stderr,"sortham Version 1.0 05/30/92 Vince Herried KA8CTE\n"
  141.       "purpose: sort input based on amateur call signs starting in column 1\n"
  142.       "usage: sortham inputfilename outputfilename\n");
  143.       exit(1);
  144.    } /* if argc */
  145.  
  146.    size=fsize(*++argv);      /* find size of the input file */
  147.  
  148.    size += size/5;         /* increase size because I add a \0 at end   */
  149.                  /* of each line. This value assumes an avg  */
  150.                  /* line length of 5 (eg 1 X 2 calls).        */
  151.                  /* With longer call signs less space is required */
  152.  
  153.    if ( size < 512 )         /* fix problems with small files              */
  154.    {
  155.       size = 512;
  156.    } /* if size */
  157.  
  158.  
  159.    input=fopen(*argv,"r");
  160.    if ( input == NULL )
  161.    {
  162.       fprintf(stderr,"SRTH003E - open failed for %s\n",*argv);
  163.       cleanup();
  164.    } /* if input=fopen(*argv,"r") */
  165.  
  166.    argv++;
  167.  
  168.    output=fopen(*argv,"w");
  169.    if ( output == NULL )
  170.    {
  171.       fprintf(stderr,"open failed for %s\n",*argv);
  172.       cleanup();
  173.    } /* if input=fopen(*argv,"w") */
  174.  
  175.    if ( size < 0 )
  176.    {
  177.       cleanup();               /* not a file, exit */
  178.    } /* if size */
  179.  
  180.    if ( !(buf = (char *) malloc(size)) )
  181.    {
  182.       fprintf(stderr,"SRTH004E - Unable to allocate memory for data buffer!\n");
  183.       cleanup();
  184.    } /* if !(buf */
  185.  
  186.    if ( !(pbuf = (char *) malloc(size)) )
  187.    {
  188.       fprintf(stderr,"SRTH005E - Unable to allocate memory for pointer buffer!\n");
  189.       cleanup();
  190.    } /* if !(pbuf */
  191.  
  192.    p=pbuf;
  193.    *p=buf;
  194.  
  195.    buf_used=0;                /* amount of buffer used so far  */
  196.    ptrs_used=0;             /* number of pointers used so far*/
  197.  
  198.  
  199.    for (numlines=0;!feof(input);++numlines)
  200.    {
  201.  
  202.       fgets(line,255,input);
  203.  
  204.       buf_used += strlen(line) +1;
  205.  
  206.       if ( buf_used >= size )
  207.       {
  208.      fprintf(stderr,"SRTH006E - oops it doesn't fit!\n");
  209.      cleanup();
  210.  
  211.       } /* if buf_used */
  212.  
  213.  
  214.       ptrs_used++;
  215.  
  216.       if ( ptrs_used >= size/4 )
  217.       {
  218.      fprintf(stderr,"SRTH007E - oops not enough room in the pointer buffer!\n");
  219.      cleanup();
  220.       } /* if ptrs_used */
  221.  
  222.  
  223.  
  224.       strcpy(*p,line);
  225.  
  226.       *++p   =    *p;
  227.  
  228.       *p   =  *p + strlen(line) +1;
  229.  
  230.    } /* for  */
  231.  
  232.    numlines--;
  233.  
  234.    fclose(input);
  235.    input=NULL;
  236.  
  237.    qsort(pbuf,numlines,sizeof(char *),cmp);
  238.  
  239.    p=pbuf;
  240.  
  241.    for (i = 0;i<numlines;++i)
  242.    {
  243.       fprintf(output,"%s",*p++);
  244.    } /* for  */
  245.  
  246.    cleanup();
  247.    return(0);
  248. } /* main () */
  249.  
  250.  
  251.